package yunjiao.javatutorials.designpatterns.builder;


import yunjiao.javatutorials.designpatterns.builder.food.beverages.BeverageSize;
import yunjiao.javatutorials.designpatterns.builder.food.beverages.CocaCola;
import yunjiao.javatutorials.designpatterns.builder.food.beverages.Pepsi;
import yunjiao.javatutorials.designpatterns.builder.food.burger.ChickenBurger;
import yunjiao.javatutorials.designpatterns.builder.food.burger.VegBurger;
import yunjiao.javatutorials.designpatterns.builder.food.nuggets.CheeseNuggets;
import yunjiao.javatutorials.designpatterns.builder.food.nuggets.ChickenNuggets;
import yunjiao.javatutorials.designpatterns.builder.meal.FoodItem;
import yunjiao.javatutorials.designpatterns.builder.meal.Meal;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单建造者
 *
 * @author yangyunjiao
 */
public class OrderBuilder {
    protected static final double HAPPY_MENU_DISCOUNT = 5.0d;
    private String customerName;
    private OrderType orderType = OrderType.TAKE_AWAY;
    private final List<FoodItem> items = new ArrayList<FoodItem>();


    public OrderBuilder name(String customerName) {
        this.customerName = customerName;
        return this;

    }

    public OrderBuilder orderType(OrderType orderType) {
        if (orderType != null) {
            this.orderType = orderType;
        }
        return this;
    }

    public OrderBuilder item(FoodItem item) {
        items.add(item);
        return this;
    }


    // Happy Menus
    public OrderBuilder vegNuggetsHappyMeal() {
        List<FoodItem> foodItems = new ArrayList<FoodItem>();
        foodItems.add(new CheeseNuggets());
        foodItems.add(new Pepsi(BeverageSize.S));
        Meal meal = new Meal("Veg Nuggets Happy Meal", foodItems, HAPPY_MENU_DISCOUNT);
        return item(meal);

    }

    public OrderBuilder chickenNuggetsHappyMeal() {
        List<FoodItem> foodItems = new ArrayList<FoodItem>();
        foodItems.add(new ChickenNuggets());
        foodItems.add(new CocaCola(BeverageSize.S));
        Meal meal = new Meal("Chicken Nuggets Happy Meal", foodItems, HAPPY_MENU_DISCOUNT);
        return item(meal);

    }

    public OrderBuilder vegBurgerHappyMeal() {
        List<FoodItem> foodItems = new ArrayList<FoodItem>();
        foodItems.add(new VegBurger());
        foodItems.add(new Pepsi(BeverageSize.S));
        Meal meal = new Meal("Veg Burger Happy Meal", foodItems, HAPPY_MENU_DISCOUNT);
        return item(meal);

    }

    public OrderBuilder chickenBurgerHappyMeal() {
        List<FoodItem> foodItems = new ArrayList<FoodItem>();
        foodItems.add(new ChickenBurger());
        foodItems.add(new CocaCola(BeverageSize.S));
        Meal meal = new Meal("Chicken Burger Happy Meal", foodItems, HAPPY_MENU_DISCOUNT);
        return item(meal);

    }

    public Order build() {
        Order order = new Order(orderType, items, customerName);
        if (!validateOrder()) {
            System.out.println("Sorry! Order can't be placed without service type (Take Away/Eat Here) and any food item.");
            return null;
        }
        return order;

    }

    private boolean validateOrder() {
        return (orderType != null) && !items.isEmpty();
    }
}
